// main layer
var g_enemyProxy=null;
var EnemyProxy = cc.Node.extend({
    _enemyBuffer : null,
    _enemyGroup : 0,
    _allEmitter : 0,
    _curEmitter : 0,
    _levelDown : false,
    ///////////////////////////
    _enemies : [],
    ctor:function ( ) {
        this._super();
        // add map
        g_enemyProxy=this;
        g_enemyProxy.retain();
        // init enemy
        this.initEnemyBuffer();

        return true;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function() {
        this._enemies.splice(0,this._enemies.length);
        this._super();
    },
    getEnemies:function(){
        return this._enemies;
    },
    isLevelFinish : function(){
        return this._levelDown;
    },
    initEnemyBuffer : function(){
        this._enemyBuffer=GetEnemyObject();
    },
    emission : function(){
        var jsonObject=this._enemyBuffer;
        if(jsonObject){
            // emitter all enemy then stop
            if( this._enemyGroup<jsonObject.emitter.length ){
                var emit = jsonObject.emitter[this._enemyGroup];
                this._curEmitter = 0;
                this._allEmitter = emit.num-1;
                this.schedule( this.interval,emit.st,this._allEmitter,emit.dl);
                cc.log(" all group: "+jsonObject.emitter.length );
                cc.log(" current group: "+(this._enemyGroup+1)+" enemies: "+emit.num );
            }
        }else{
            cc.log(" enemy object invalid from GetEnemyObject");
        }
    },
    interval : function(dt){
        var jsonObject=this._enemyBuffer;
        var emit = jsonObject.emitter[this._enemyGroup];
        var filename = MainResource(emit.res[0]);
        var enemy = Enemy.create(filename,jsonObject.direct,emit.hp,emit.sp,emit.gold);
        var bornPos = cc.p(jsonObject.x,jsonObject.y );
        enemy.setAnimateRes(emit.res);
        //enemy.setAnchorPoint( cc.p(0.5,0.0) );
        enemy.setPosition( bornPos );

        PlayEnemyBornEffect( cc.pAdd(bornPos,cc.p(0,-15) ) );

        var filename = AudioResource("born.mp3");
        cc.audioEngine.playEffect(filename);

        this.addChild(enemy,15);
        enemy.playAnimate(emit.rec[0],emit.rec[1],emit.sp);
        this._enemies.push(enemy);
        if( ++this._curEmitter > this._allEmitter ){
            if(++this._enemyGroup>=jsonObject.emitter.length){
                this._enemyGroup = 0;
                this._levelDown = true;
                cc.log("level enemy emitter down");
            }else{
                cc.log("group enemy emitter down,increment current enemy group");
            }
        }
    },
    enemyDead:function(enemy,i){
        enemy.removeFromParent(true);
        this._enemies.splice(i,1);
    },
    emptyEnemy : function(){
        return  this._enemies.length>0?false:true;
    }
});
EnemiesDead=function(enemy,i){
    if(g_enemyProxy){
        g_enemyProxy.enemyDead(enemy,i);
    }else{
        cc.log("g_enemyProxy is invalid");
    }
}
EmptyEnemy=function(){
    if(g_enemyProxy){
        return g_enemyProxy.emptyEnemy();
    }else{
        cc.log("g_enemyProxy is invalid");
    }
}
LevelFinish=function(){
    if(g_enemyProxy){
        var finish=g_enemyProxy.isLevelFinish();
        cc.log("is this leve finish: "+finish);
        return finish;
    }else{
        cc.log("g_enemyProxy is invalid");
    }
}
EmissionEnemy=function(){
    if(g_enemyProxy){
        g_enemyProxy.emission();
        cc.log("emission enemies");
    }else{
        cc.log("g_enemyProxy is invalid");
    }
}